/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.report;

import entity.Message;
import entity.RptFinishJob;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import session.ReportFacade;

/**
 *
 * @author owtee
 */
public class FinishJob extends HttpServlet {

    @EJB
    private ReportFacade reportFacade;

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            try {
                String txtExpectedDate = (request.getParameter("txtExpectedDate") != null
                        ? request.getParameter("txtExpectedDate")
                        : "");

                // Set date                
                SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
                Date dExpectedDate = new Date();

                if (txtExpectedDate.equals("")) {
                    Calendar calendar = Calendar.getInstance();
                    // Clear time
                    calendar.set(Calendar.HOUR_OF_DAY, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);
                    calendar.set(Calendar.MILLISECOND, 0);
                    // Set today                    
                    dExpectedDate = calendar.getTime();

                    txtExpectedDate = dateFormat.format(dExpectedDate);
                } else {
                    dExpectedDate = dateFormat.parse(txtExpectedDate);
                }

                List<Object> rstFinishJob = reportFacade.callSpMsgGetFinishJob(dExpectedDate);
                List<RptFinishJob> rptFinishJobLst = new ArrayList<RptFinishJob>();

                for (int i = 0; i < rstFinishJob.size(); i++) {
                    Object[] column = (Object[]) rstFinishJob.get(i);
                    /*
                     for (int j = 0; j < column.length; j++) {
                     out.println("row[" + i + "]column[" + j + "] = " + column[j].toString() + "<br/>");
                     }
                     */
                    RptFinishJob rptFinishJob = new RptFinishJob();
                    rptFinishJob.setExpectedDateFormat(txtExpectedDate);
                    rptFinishJob.setCloseJob(Integer.parseInt(column[0].toString()));
                    rptFinishJob.setNotCloseJob(Integer.parseInt(column[1].toString()));
                    rptFinishJob.setTotalJob(Integer.parseInt(column[2].toString()));
                    rptFinishJobLst.add(rptFinishJob);
                }

                request.setAttribute("rptFinishJobLst", rptFinishJobLst);
                request.setAttribute("txtExpectedDate", txtExpectedDate);

            } catch (Exception ex) {
                Message message = new Message();
                message.setMesDetail(ex.getMessage());
                request.setAttribute("message", message);
            }

            RequestDispatcher rd = request.getRequestDispatcher("finish_job.jsp");
            rd.forward(request, response);

        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
